/*
 * thread.cpp
 *
 *  Created on: 22/05/2011
 *      Author: wadi
 */

#include "thread.h"

Thread::Thread() : wasStarted(false) {
}

void * Thread::starter(void* object){
    Thread* t=(Thread*)object;
    t->run();
    return 0;
}

Thread::~Thread() {
	if (wasStarted)
		pthread_attr_destroy(&attr);
}

void Thread::start(){
    // Para mayor portabilidad se crear hilos que admitan joins
    // en forma explicita
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    // Creamos el hilo para que llame al metodo estatico starter
    pthread_create(&thread,&attr,Thread::starter,(void*)this);
    wasStarted = true;
}


void Thread::join(){
    pthread_join(thread,NULL);
}
